<?
	  layout("/inc/win_layout.html",{title:sysTitle}){
    ?>
<div style="display:none;">
	点播上传配置
	    <table frame=void width="400px">
	        <tr>
	            <td>uploadAuth:</td>
	            <td><input type="text" id="uploadAuth" size="20" value=""></td>
	            <td>uploadAddress:</td>
	            <td><input type="text" id="uploadAddress" size="20" value=""></td>
	        </tr>
	    </table>
	    OSS上传配置
	    <table frame=void width="400px">
	        <tr>
	            <td>accessKeyId:</td>
	            <td><input type="text" id="accessKeyId" size="20" value="${accessKeyId}"></td>
	            <td>accessKeySecret:</td>
	            <td><input type="text" id="accessKeySecret" size="40" value="${accessKeySecret}"></td>
	        </tr>
	        <tr>
	            <td>secretToken:</td>
	            <td><input type="text" id="secretToken" size="40" value="${securityToken}"></td>
	            <td>expireTime:</td>
	            <td><input type="text" id="expireTime" size="20" value="${expiration}"></td>
	        </tr>
	        <tr>
	            <td>endpoint:</td>
	            <td><input type="text" id="endpoint" size="40" value="http://oss-cn-hangzhou.aliyuncs.com"></td>
	            <td>bucket:</td>
	            <td><input type="text" id="bucket" size="20" value="${bucket_source}"></td>
	        </tr>
	        <tr>
	            <td>object路径:</td>
	            <td>
	            	 <?if(original_file == null || original_file == ''){?>
	          	  	 <input type="text" id="objectPre" size="20" value="/">
	            <?}else{?>
	            	 <input type="text" id="objectPre" size="20" value="${original_file}">
	            <?}?>
	            </td>
	        </tr>
	    </table>
	</div>	
	<div>
    <input style="float: left;margin-left: 15px;margin-top: 20px;" type="file" name="file" id="files" multiple/>
	   <!--  <button type="button" onclick="deleteFile()">删除文件</button>
	    <input type="text" id="deleteIndex" size="3" value="0">
	    <button type="button" onclick="cancelFile()">取消文件</button>
	    <input type="text" id="cancelIndex" size="3" value="0">
	    <button type="button" onclick="resumeFile()">恢复文件</button>
	    <input type="text" id="resumeIndex" size="3" value="0">
	    	列表管理
	    <button type="button" onclick="getList()">获取上传列表</button>
	    <button type="button" onclick="clearList()">清理上传列表</button> -->
    <button style="margin-left: 15px;margin-top: 20px;" type="button" onclick="start()">开始上传</button>
    <!-- <button type="button" onclick="stop()">停止上传</button> -->
    <!-- <button type="button" onclick="resumeWithToken()">Token恢复上传</button> -->
    </div>
    <div style="float: left;">
	    <div style="width:340px; height:335px;margin-left: 15px;margin-top: 20px;float: left;">
	    	<div>日志：<button style="float: right;" type="button" onclick="clearLog()">清日志</button></div>
	    	<select multiple="multiple" id="textarea" style="position:relative; width:340px; height:310px; vertical-align:top; border:1px solid #cccccc;overflow:auto;margin-top: 10px;"></select>
	    </div>
	    <div style="width:340px; height:330px;margin-left: 65px;margin-top: 50px;float: left;">
	    	<div>文件格式：</div>
	    	<div style="margin-top: 40px;">
	    		<div style="margin-top: 10px;">
	    			<div>video_name 视频名称：</div>
	    			<div style="margin-top: 10px;">
	    				<input type="text" id="video_name" name="video_name" class="form-control" />
	    			</div>
	    		</div>
	    		<div style="margin-top: 30px;">
	    			<div>audio_name 音频名称：</div>
	    			<div style="margin-top: 10px;">
	    				<input type="text" id="audio_name" name="audio_name" class="form-control" />
	    			</div>
	    		</div>
	    		<div style="margin-top: 30px;">
	    			<div>original_name 原文件名称：</div>
	    			<div style="margin-top: 10px;">
	    				<input type="text" id="original_name" name="original_name" class="form-control" />
	    			</div>
	    		</div>
	    	</div>
	    </div>
    </div>
<?} ?>
<script src="static/js/aliyun-sdk.min.js"></script>
<script src="static/js/vod-sdk-upload-1.0.7.min.js"></script>
<script type="text/javascript">
	function getQueryString(name) { 
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
		var r = window.location.search.substr(1).match(reg); 
		if (r != null) return unescape(r[2]); return null; 
	} 
	var winName=getQueryString("winName");
	var aliyunId = window.parent.window[winName].getaliyunId();	
	var aliyunUploadingData = window.parent.window[winName].getAliyunUploadingData();	
	var fileName = '';
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	var uploader;
	$(function(){
		if(null != aliyunUploadingData && '' != aliyunUploadingData){
			var jsonObj =  JSON.parse(aliyunUploadingData);//转换为json对象
			for(var i=0;i<jsonObj.length;i++){
				$("#video_name").val(jsonObj[i].video_name);
				$("#audio_name").val(jsonObj[i].audio_name);
				$("#original_name").val(jsonObj[i].original_name);
			}
		}
	});
	    window.onload = new function() {
	        uploader = new VODUpload({
	            // 文件上传失败
	            'onUploadFailed': function (uploadInfo, code, message) {
	                log("文件上传失败:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
	            },
	            // 文件上传完成
	            'onUploadSucceed': function (uploadInfo) {
	            	 log("*********:"+JSON.stringify(uploadInfo)+":**************");	
	            	
	             	  log("文件上传完成: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
	            },
	            // 文件上传进度
	            'onUploadProgress': function (uploadInfo, totalSize, uploadedSize) {
	                log("文件上传进度:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(uploadedSize * 100 / totalSize) + "%");
	            },
	            // STS临时账号会过期，过期时触发函数
	            'onUploadTokenExpired': function () {
	                log(" STS临时账号会过期，过期时触发函数");
	                if (isVodMode()) {
	                    // 实现时，从新获取UploadAuth
	                    // uploader.resumeUploadWithAuth(uploadAuth);
	                } else if (isSTSMode()) {
	                    // 实现时，从新获取STS临时账号用于恢复上传
	                    // uploader.resumeUploadWithToken(accessKeyId, accessKeySecret, secretToken, expireTime);
	                }
	            },
	            // 开始上传
	            'onUploadstarted': function (uploadInfo) {
	                if (isVodMode()) {
	                    var uploadAuth = document.getElementById("uploadAuth").value;
	                    var uploadAddress = document.getElementById("uploadAddress").value;
	                    uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress);
	                }
	                log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
	            }
	        });

	        var accessKeyId = document.getElementById("accessKeyId").value;
	        var accessKeySecret = document.getElementById("accessKeySecret").value;
	        var secretToken = document.getElementById("secretToken").value;
	        var expireTime = document.getElementById("expireTime").value;

	        if (isVodMode()) {
	            // 点播上传。每次上传都是独立的鉴权，所以初始化时，不需要设置鉴权
	            uploader.init();
	        } else if (isSTSMode()) {
	            // OSS直接上传:STS方式，安全但是较为复杂，建议生产环境下使用。
	            // 临时账号过期时，在onUploadTokenExpired事件中，用resumeWithToken更新临时账号，上传会续传。
	            uploader.init(accessKeyId, accessKeySecret, secretToken, expireTime);
	        } else {
	            // OSS直接上传:AK方式，简单但是不够安全，建议测试环境下使用。
	            uploader.init(accessKeyId, accessKeySecret);
	        }
	    };

	    document.getElementById("files")
	        .addEventListener('change', function (event) {
	            var endpoint = document.getElementById("endpoint").value;
	            var bucket = document.getElementById("bucket").value;
	            var objectPre = document.getElementById("objectPre").value;
	            var userData;
		        for(var i=0; i<event.target.files.length; i++) {
		            if (isVodMode()) {
		                userData = '{"Vod":{"UserData":"{"IsShowWaterMark":"false","Priority":"7"}"}}';
		            } else {
		                userData = '{"Vod":{"Title":"'+event.target.files[i].name+'","Description":"","CateId":"19",\
		                "Tags":"tag1,tag2,标签3","UserData":"user data"}}';
		            }
	            	
	                log("添加文件: " + event.target.files[i].name);
	                fileName = event.target.files[i].name;
	                if (isVodMode()) {
	                    // 点播上传。每次上传都是独立的OSS object，所以添加文件时，不需要设置OSS的属性
	                    uploader.addFile(event.target.files[i], null, null, null, userData);
	                } else {
	                    uploader.addFile(event.target.files[i], endpoint, bucket, objectPre + event.target.files[i].name, userData);
	                }
	            }
	        });

	    var textarea=document.getElementById("textarea");

	    function start() {
	        log("开始 upload.");
	        uploader.startUpload();
	        var videoName = fileName.substr(0,fileName.lastIndexOf("."));
	        $("#video_name").val(videoName+".mp4");
	        $("#original_name").val(fileName);
	        if("${audio_full}" != null && '' != "${audio_full}"){
	        	 $("#audio_name").val(videoName+".mp3");
	        }
	    }

	    function stop() {
	        log("停止 upload.");
	        uploader.stopUpload();
	    }

	    function resumeWithToken() {
	        log("resume upload with token.");
	        var uploadAuth = document.getElementById("uploadAuth").value;

	        var accessKeyId = document.getElementById("accessKeyId").value;
	        var accessKeySecret = document.getElementById("accessKeySecret").value;
	        var secretToken = document.getElementById("secretToken").value;
	        var expireTime = document.getElementById("expireTime").value;

	        if (isVodMode()) {
	            uploader.resumeUploadWithAuth(uploadAuth);
	        } else if (isSTSMode()) {
	            uploader.resumeUploadWithToken(accessKeyId, accessKeySecret, secretToken, expireTime);
	        }
	    }

	    function clearList() {
	        log("clean upload list.");
	        uploader.cleanList();
	    }

	    function getList() {
	        log("get upload list.");
	        var list = uploader.listFiles();
	        for (var i=0; i<list.length; i++) {
	            log("file:" + list[i].file.name + ", status:" + list[i].state + ", endpoint:" + list[i].endpoint + ", bucket:" + list[i].bucket + ", object:" + list[i].object);
	        }
	    }

	    function deleteFile() {
	        if (document.getElementById("deleteIndex").value) {
	            var index = document.getElementById("deleteIndex").value
	            log("delete file index:" + index);
	            uploader.deleteFile(index);
	        }
	    }

	    function cancelFile() {
	        if (document.getElementById("cancelIndex").value) {
	            var index = document.getElementById("cancelIndex").value
	            log("cancel file index:" + index);
	            uploader.cancelFile(index);
	        }
	    }

	    function resumeFile() {
	        if (document.getElementById("resumeIndex").value) {
	            var index = document.getElementById("resumeIndex").value
	            log("resume file index:" + index);
	            uploader.resumeFile(index);
	        }
	    }

	    function clearLog() {
	        textarea.options.length = 0;
	    }

	    function log(value) {
	        if (!value) {
	            return;
	        }

	        var len = textarea.options.length;
	        if (len > 0 && textarea.options[len-1].value.substring(0, 40) == value.substring(0, 40)) {
	            textarea.remove(len-1);
	        } else if (len > 25) {
	            textarea.remove(0);
	        }

	        var option=document.createElement("option");
	        option.value=value,option.innerHTML=value;
	        textarea.appendChild(option);
	    }

	    function isVodMode() {
	        var uploadAuth = document.getElementById("uploadAuth").value;
	        return (uploadAuth && uploadAuth.length > 0);
	    }

	    function isSTSMode() {
	        var secretToken = document.getElementById("secretToken").value;
	        var expireTime = document.getElementById("expireTime").value;
	        if (!isVodMode()) {
	            if (secretToken && secretToken.length > 0 && expireTime && expireTime.length > 0) {
	                return true;
	            }
	        }
	        return false;
	    }
	
	function queren(){
		var video_name = $("#video_name").val();
		var audio_name = $("#audio_name").val();
		var original_name = $("#original_name").val();
		var dataValue = [{"video_name": video_name,"audio_name": audio_name,"original_name":original_name}];
		window.parent.window[winName].$("#dictitemId_"+aliyunId).val(JSON.stringify(dataValue));
	    window.parent.window[winName].$("#dictitemName_"+aliyunId).val(JSON.stringify(dataValue));
	   	parent.layer.tips("Ok", "#t_"+aliyunId, {time: 5000});
		parent.layer.close(index);
		
	}
</script>